<?php


/**
 * Every informations available about a kit
 */
class KitBean extends DataBean {

    public function __construct($id) {
        $this->id=$id;
        parent::__construct();
        // getting kit
		try {
            // loading kit cfg
            $cfg=Webappkit::getCfgFromId($id); // print_r($cfg);
            $this->desc=$cfg['kit']['desc'];
            $this->link=$cfg['kit']['link'];
            $this->version=$cfg['kit']['version'];
            $this->phpversion=$cfg['kit']['phpversion'];
            $this->dependencies=$cfg['load_kits'];
            // listing subkits
            $subkits=Webappkit::listKits(Webappkit::getPathFromId($id));
            foreach ($subkits as $subkit) {
                $subid=$id.'.'.$subkit; // echo "<p>$subid</p>";
                $this->subkits[$subkit]=new KitBean($subid);
            }
            // has tests ?
            $this->has_tests=isset($cfg['tools']['tests']);
            // diagnostics
            // trying to load kit
            Webappkit::getkit($id);
			/*if (!$kit=Webappkit::getKit($id)) {
				// throw new WakEngineErrorPage("kit not found",404);
			}*/
            // $this->diagno
		} catch (WakBadKit $e) {
			$this->error=$e;
		} catch (WakBadCfg $e) {
            $this->error=$e;
        } catch (WakMissingKit $e) {
            $this->error=$e;
        } catch (WakBadPhp $e) {
            $this->error=$e;
        } catch (WakMissingFile $e) {
            $this->error=$e;
        }
    }

    public $id;

    public $kit;
    function _kit_get() {
        if ($this->kit)
            return $this->kit;
        try {
            $this->kit=Webappkit::getKit($this->id);
		} catch (WakBadKit $e) {
			$this->error=$e;
		} catch (WakBadCfg $e) {
            $this->error=$e;
        } catch (WakMissingKit $e) {
            $this->error=$e;
        } catch (WakBadPhp $e) {
            $this->error=$e;
        } catch (WakMissingFile $e) {
            $this->error=$e;
        }
        return $this->kit;
    }

    public $desc;

    public $link;

    public $version;

    public $phpversion;

    public $dependencies=array();

    /**
     * @public array id => KitBean
     */
    public $subkits=array();

    public $has_tests=false;

    public $tests_report;
    function _tests_report_get() {
        if (!isset($this->tests_report)) {
            $kit=$this->get('kit');
            if ($kit->tests)
                $this->tests_report=$kit->tests->run();
        }
        return $this->tests_report;
    }

    public $error;

    public $docs=array();
    function _docs_autoValue() {
        $path=Webappkit::getpathfromid($this->id).'docs/'; //echo $path;
        if (file_exists($path)) {
            $dir=dir($path);
            while ($file=$dir->read()) {
                if (substr($file,0,1)!='.')
                    $this->docs[]=$file;
            }
        }
        // print_r($this->docs);
    }
}
